<template>
  <div class="login">
    <div class="left">
      <div class="img"></div>
    </div>
    <div class="right">
      <div class="login-box">
        <img
          src="/src/core/assets/images/logo/logo.png"
          alt="logo"
          class="logo"
        />
        <div class="wellcome-title">Hi , 欢迎登录平台 👏</div>

        <!-- 表单 -->
        <div class="login-form">
          <el-form
            :model="form"
            :rules="rules"
            ref="formRef"
            size="large"
            label-position="top"
          >
            <el-form-item label="用户名" prop="username">
              <el-input v-model="form.username" placeholder="输入用户名" />
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input
                type="password"
                v-model="form.password"
                placeholder="输入密码"
              />
            </el-form-item>
            <el-form-item>
              <el-checkbox v-model="form.remember" label="记住账户" />
            </el-form-item>
            <el-form-item>
              <el-button
                type="primary"
                :loading="loading"
                class="login-btn"
                @click="onSubmit"
              >
                登录
              </el-button>
            </el-form-item>
            <div class="rights">©2023 fanadmin All Rights Reserved.</div>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
// 引入styles
import '@/core/styles/default/index.scss'
import { computed, reactive, ref } from 'vue'
import { ElMessage, FormRules } from 'element-plus'
import useTabsStore from '@/core/store/modules/tabs.ts'
import { initDynamicRouter } from '@/core/router/modules/dynamicRouter.ts'
import useUserStore from '@/core/store/modules/user.ts'
import userUserInfoStore from '@/core/store/modules/userInfo.ts'
import { useRouter } from 'vue-router'
import { getInitConfig, login } from '@/core/api/auth'

const userStore = useUserStore()
const userInfoStore = userUserInfoStore()
const router = useRouter()
const tabsStore = useTabsStore()

// 获取配置
const getConfig = async () => {
  try {
    await getInitConfig()
  } catch (e) {}
}
getConfig()

const appConfig = computed(() => {
  return ''
})
const loading = ref(false)

const formRef = ref(null)

const form = reactive({
  username: '',
  password: '',
  remember: false,
})
const rules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 12, message: '密码长度在 6 到 12 个字符', trigger: 'blur' },
  ],
}
const onSubmit = async () => {
  if (!form) return
  formRef.value.validate(async (valid) => {
    console.log(valid, 'valid')
    if (valid) {
      try {
        loading.value = true
        // 1、执行登录接口
        const res = await login(form)
        userStore.setToken(res.data.token)
        userInfoStore.setUserInfo(res.data.admin)
        // 2、添加动态路由 AND 用户按钮 AND 角色信息 AND 用户个人信息
        await initDynamicRouter()
        // 3、清空 tabs数据、keepAlive缓存数据
        tabsStore.setTab([])
        // 4、跳转到首页
        ElMessage('登录成功🌻')
        setTimeout(() => {
          loading.value = false
          router.push('/admin/welcome')
        }, 1500)
      } catch (error) {
        setInterval(() => {
          loading.value = false
        }, 1000)
      }
    } else {
      ElMessage('校验失败，信息填写有误🌻')
    }
  })
}
</script>

<style lang="scss" scoped>
.demonstration {
  color: var(--el-text-color-secondary);
}

.el-carousel__item h3 {
  color: #475669;
  opacity: 0.75;
  //line-height: 200px;
  margin: 0;
  text-align: center;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}

.login {
  display: flex;
}

.left {
  width: 100%;
  height: 100vh;
  .img {
    background-image: url('/src/core/assets/images/login/bg.png');
    background-size: cover;
    background-repeat: no-repeat;
    background-position: center;
    height: 100%;
    //width: 100%;
  }
}

.right {
  display: flex;
  justify-content: center;
  align-items: center;
  flex: 1;
  padding: 30px 90px;
  position: relative;
}

.login-box {
  width: 400px;

  .logo {
    width: 50px;
    height: 50px;
  }
}

.login-box .wellcome-title {
  font-size: 32px;
  font-weight: 500;
  margin: 32px 0 24px;
}

.login-box .subtitle {
  color: #999;
  font-size: 10px;
  margin-bottom: 30px;
}

.login-form {
  text-align: center;
}

.login-btn {
  width: 100%;
}

.rights {
  margin: 32px auto;
  text-align: center;
  height: 32px;
  line-height: 32px;
  font-size: 12px;
  color: #8e919e;
}
</style>
